

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.ComponentUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map.Entry;



public class GUI extends JFrame {

	public JPanel panel = new JPanel();
	int currentPage;
	int totalPages;
	int backPanel = 3;
	JPanel panel2;
	private static final int IMG_WIDTH = 149;
	private static final int IMG_HEIGHT = 117;
	int flag = 0, flag1 = 0, flag2= 0 , flag3 = 0, flag4 = 0;


	
//	String fileContents = IOUtils.toString(new FileInputStream("c:\\project"));
	File theDirectory = new File("c:\\project");
	ArrayList<ResultImage> resultList;
	Integer numOfClusters = 100;
	Integer maxClusterSize = 100;
	Integer numOfItr = 100;
	Integer numOfImg = 44;
	Integer matchPrec = 0;
	JButton chooseImg = null;
	JButton searchButton;
	JComboBox dataBaseCombo = null;
	JPanel panel3 = null;
	JPanel panel4 = null;
	String DataBaseID = "Data Base";
	JTextField numOfClustersText =  null;
	JTextField maxClusterSizeText = null;
	JTextField numOfItrText = null;
	JTextField numOfImgText =  null;
	JTextField matchPrecText = null;
	JTextField TextDbId = null;
 	JFileChooser fileChooser = new JFileChooser();
 	ImageFileChooser imgChooser = new ImageFileChooser();
 	
 	File file;
	File imgFile;
	JTextField TextPath = null;
	String selectedDB;

	
    public GUI() {
        initializeUI();
    }

    private void initializeUI() {
    	setLayout(null);
        setSize(650, 700);
        setLocation(450, 0);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel(null);
        panel = createPanel(1);
        setContentPane(panel);
    }
          
    public JPanel createPanel(int i)
    {
	 
	 switch (i) {
	 
	 case 1:
		 	return FirstPanel();
	 case 2:
		 	return BuildDataBasePanel();
	 case 3:
		 	return VisualSearch();
	 case 4:
		 	return ResultPanel(); 
		 	
	 }
	 return null;
     
    }
    
    private JPanel ResultPanel() {
    	
    	JLabel headline = new JLabel("Efficient visual search of videos cast as text retrieval");
  		headline.setFont(new Font("Serif", Font.BOLD, 26));
  	    headline.setBounds(20, 60, 1000, 100);
  	    
  	    ImageIcon icon = new ImageIcon("c:/project/crop/BraudeLogo.JPG");
  	    JLabel Label1 = new JLabel(icon, JLabel.CENTER);
  		Label1.setBounds(210, 7, 250, 100);
  		
  		panel4 = new JPanel();
	 	panel4.setLayout(null);
	 	panel4.setBackground(Color.WHITE);
	 
  		
    	String pages = "";
    	for (int i=0; i<totalPages; i++)
    		if (i!= currentPage)
    			pages = pages +"  " + i;
    		pages = pages +"  ";
    	int x = 70;
    	int y = 200;
    	for (int j = 0; j < 6 ;j++)
    	{
    		if ((j+currentPage*6)<resultList.size())
    		{
	    		if (j == 3)
	    		{
	    			y = 400;
	    			x = 70;
	    		}
	    		BufferedImage bImage = null;
	    		try { bImage = ImageIO.read(new File(resultList.get(j+currentPage*6).getImg().getLocation())); } catch (IOException e) { }
	    		bImage = resizeImage(bImage,bImage.getType());
	    		 ImageIcon img = new ImageIcon(bImage);
	    		 JLabel ImgLbl = new JLabel(img);
	    		 ImgLbl.setBounds(x, y, 149, 117);
	    		 Double sim = resultList.get(j+currentPage*6).getSim();
	    		 sim = sim*100;
	    		 String s[] = sim.toString().split("\\.");
	    		 JLabel precLbl = new JLabel(s[0] + " %");
	    		 precLbl.setBounds(x+63, y+105, 149, 50);
	    		 x = x + 170;
	    		 panel4.add(ImgLbl);
	    		 panel4.add(precLbl);
    		}
    	}
    	
    	if ((currentPage*6+6)<resultList.size())
    	{
	    	JButton next = new JButton();
	    	next.setText("Next");
	    	next.setBounds(450, 550, 100, 30);
			next.addActionListener(new ActionListener() {
				 
				 
			        public void actionPerformed(ActionEvent e)
			        {
			    		currentPage++;
			            //Execute when button is pressed
			            System.out.println("You clicked the 'Back' button");
			            panel = createPanel(4);
			        	setContentPane(panel);             
				        setVisible(true);
			        }
			    });  
			panel4.add(next);
    	}
    	
		 if (currentPage!=0)
		 {
			 JButton prev = new JButton();
			 prev.setText("Prev");
			 prev.setBounds(75, 550, 100, 30);
			 prev.addActionListener(new ActionListener() {
				 
				 
			        public void actionPerformed(ActionEvent e)
			        {
						 currentPage--;
			            //Execute when button is pressed
			            System.out.println("You clicked the 'Back' button");
			            panel = createPanel(4);
			        	setContentPane(panel);             
				        setVisible(true);
			        }
			    });    
			 panel4.add(prev);
			 
		 }
    	 JButton button4 = new JButton();
		 button4.setText("Back");
		 button4.setBounds(265, 600, 100, 30);
		 button4.addActionListener(new ActionListener() {
			 
			 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		            System.out.println("You clicked the 'Back' button");
		            panel = createPanel(3);
		        	setContentPane(panel);             
			        setVisible(true);
		        }
		    });    
		 
		
		panel4.add(button4); 
    	panel4.add(headline);
    	panel4.add(Label1);
		return panel4;
	}

	public JPanel FirstPanel()
    {
	 	JPanel panel1 = new JPanel(null);
	 	panel1.setLayout(null);
	 	panel1.setBackground(Color.WHITE);
	 	
	    JLabel headline = new JLabel("Efficient visual search of videos cast as text retrieval");
		headline.setFont(new Font("Serif", Font.BOLD, 26));
	    headline.setBounds(20, 60, 1000, 100);
	    
	    ImageIcon icon = new ImageIcon("c:/project/crop/BraudeLogo.JPG");
	    JLabel Label1 = new JLabel(icon, JLabel.CENTER);
		Label1.setBounds(210, 7, 250, 100);
		
		ImageIcon icon1 = new ImageIcon("c:/project/crop/Flowers.JPG");
		JLabel Label2 = new JLabel(icon1, JLabel.CENTER);
		Label2.setBounds(70, 175, 537, 297);
			
		
		JButton button1 = new JButton();
	    button1.setText("Build Data Base");
	    button1.setBounds(120, 500, 130, 30);
	    button1.addActionListener(new ActionListener() {
	    	 
	        public void actionPerformed(ActionEvent e)
	        {
	            //Execute when button is pressed
	            System.out.println("You clicked the 'Build Data Base' button");
	            //remove(panel);
	        	panel = createPanel(2);
	        	setContentPane(panel);             
		        setVisible(true);
	        }
	    });
	
	    JButton button2 = new JButton();
	    button2.setText("Visual Search");
	    button2.setBounds(370, 500, 130, 30);
	    button2.addActionListener(new ActionListener() {
	    	 
	        public void actionPerformed(ActionEvent e)
	        {
	            //Execute when button is pressed
	            System.out.println("You clicked the 'Visual Search' button");
	            //remove(panel);
	        	panel = createPanel(3);
	        	setContentPane(panel);             
		        setVisible(true);
	        }
	    });
	    
	    JButton close = new JButton("Close");
	    close.setBounds(265, 600, 100, 30);
	    close.addActionListener(new ActionListener() {
	    	 
	        public void actionPerformed(ActionEvent e)
	        {
	            //Execute when button is pressed
	            System.out.println("Closing");
	            System.exit(0);
	        }
	    });
	
	
	    panel1.add(headline);
	    panel1.add(Label1);
	    panel1.add(Label2);
	    panel1.add(button1);
	    panel1.add(button2);
	    panel1.add(close);
	
	    return panel1;
    }
    
	public JPanel BuildDataBasePanel()
    {

    	ImageIcon icon = new ImageIcon("c:/project/crop/BraudeLogo.JPG");
	    JLabel Label1 = new JLabel(icon, JLabel.CENTER);
		Label1.setBounds(210, 7, 250, 100);
		
		/////////////////////////////////////////////////////////////////////////////////
/*		JPanel panel1 = new JPanel(null);
	 	panel1.setLayout(null);
	 	panel1.setBackground(Color.WHITE);
	 	
	    JLabel headline = new JLabel("Efficient visual search of videos cast as text retrieval");
		headline.setFont(new Font("Serif", Font.BOLD, 26));
	    headline.setBounds(20, 60, 1000, 100);
	    
	    ImageIcon icon = new ImageIcon("BraudeLogo.JPG");
	    JLabel Label1 = new JLabel(icon, JLabel.CENTER);
		Label1.setBounds(210, 7, 250, 100);
		
		ImageIcon icon1 = new ImageIcon("Flowers.JPG");
		JLabel Label2 = new JLabel(icon1, JLabel.CENTER);
		Label2.setBounds(70, 175, 537, 297);
			

		
		*/
	
		
		

		
		
		
		
		JButton helpButton = new JButton();
		helpButton.setText("Help");
		helpButton.setBounds(20, 20, 60, 30); 
		helpButton.addActionListener(new ActionListener() {
	    	 
	        public void actionPerformed(ActionEvent e)
	        {
	        	JFrame helpFrame = new JFrame();
	        	helpFrame.setSize(440, 550);
	        	helpFrame.setLocation(450, 100);       
	            JPanel helpPanel = new JPanel(null);
	            helpPanel.setLayout(null);
	            helpPanel.setBackground(Color.WHITE);
	            
	            ImageIcon helpIcon = new ImageIcon("c:/project/crop/BuildDBhelp.png");
	    	    JLabel Label1 = new JLabel(helpIcon, JLabel.CENTER);
	    		Label1.setBounds(0, 0, 425, 550);
	            
	    		helpPanel.add(Label1);
	            helpFrame.add(helpPanel);
	            helpFrame.setVisible(true);
	          
	     

	        }
	    });
		////////////////////////////////////////////////////////////////////////////////
		JLabel lblID = new JLabel("DataBase Name:");
		 	lblID.setBounds(55, 250, 128, 25);
				
		 	
		 	JLabel lblFolder = new JLabel("Folder:");
			lblFolder.setBounds(55, 200, 128, 25);
		
		 	TextPath = new JTextField();
		 	TextPath.setBounds(new Rectangle(220, 200, 150, 25));
		 	TextPath.setText("Folder");
		 	TextPath.setEnabled(false);
			
		 	
		 	JProgressBar pbProgress = new JProgressBar();
            //setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(pbProgress, gbc);

            gbc.gridy++;
		 	
		 	JButton chooseDB = new JButton();
		 	chooseDB.setText("choose database");
		 	chooseDB.setBounds(400, 200, 150, 30);
		 	

		 	chooseDB.addActionListener(new ActionListener() {
		    	 
		 		
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		            System.out.println("You clicked the 'buildDB' button");
		        

		            
		         	fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		         	fileChooser.setCurrentDirectory(theDirectory);
				 	Integer opt = fileChooser.showSaveDialog(fileChooser);	
				 	file = fileChooser.getSelectedFile();
				 	TextPath.setText(file.getPath());
				 	TextPath.setEnabled(true);
				 	

		        }
		    });
		
		 	
		
		
		 	
			TextDbId = new JTextField();
			TextDbId.setBounds(new Rectangle(220, 250, 150, 25));
			TextDbId.setText("Data Base");	
			TextDbId.addFocusListener(new java.awt.event.FocusAdapter() 
			{
				public void focusGained(FocusEvent e){
					TextDbId.setText("");
		        }
				
				public void focusLost(java.awt.event.FocusEvent e) 
				{
					//System.out.println("focusLost()"); 
					
					DataBaseID=TextDbId.getText();			
				}
			}
			);
			
			 panel2 = new JPanel();
			 panel2.setLayout(null);
			 panel2.setBackground(Color.WHITE);
			 
		 
		 	JButton buildDB = new JButton();
		 	buildDB.setText("Build database");
		 	buildDB.setBounds(250, 450, 120, 30);
		 	buildDB.addActionListener(new ActionListener() {
		    	 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		            //remove(panel);
		            if (TextPath.getText().equals("Folder"))
		     	   		JOptionPane.showMessageDialog(null,"You must choose a folder","Error",JOptionPane.ERROR_MESSAGE);
		            DataBase DB = new DataBase(file.getPath(),DataBaseID , numOfClusters , maxClusterSize, numOfItr );
		            if (DB.CheckID()!=1)
		            	flag4 = 1;
		            else
		            	flag4 = 0;
		            flag = flag4+flag1+flag2+flag3;
		            switch (flag)
		            {
		            	case 0:
		            		SwingWorkerProgress s = new SwingWorkerProgress(DB);
			     	   		JOptionPane.showMessageDialog(null,"Building dataBase, this will take a few minutes","Building dataBase",JOptionPane.INFORMATION_MESSAGE);
			     	   		JProgressBar pb = s.tp.pbProgress;
			     	   		pb.setBounds(new Rectangle(120, 500, 400, 14));
			     	   		panel2.add(pb);
			     	   		s.tp.po(pb);
			     	   		panel2.repaint();
			     	   		break;
		            	
		            	case 1:
			     	   		JOptionPane.showMessageDialog(null,"This Database name os already exist ","Error",JOptionPane.ERROR_MESSAGE);
			     	   		break;
		            	default:
			     	   		JOptionPane.showMessageDialog(null,"Enter only Integers","Error",JOptionPane.ERROR_MESSAGE);
			     	   		break;
		            	
		            }
		        }
		    });
		 
			JLabel lblNumOfClusters = new JLabel("Number of clusters:");
		 	lblNumOfClusters.setBounds(55, 300, 128, 25);
			numOfClustersText = new JTextField();
			numOfClustersText.setBounds(new Rectangle(220, 300, 150, 25));
			numOfClustersText.setText("100");
			numOfClustersText.addFocusListener(new java.awt.event.FocusAdapter() 
			{
				public void focusGained(FocusEvent e){
					numOfClustersText.setText("");
		        }
				public void focusLost(java.awt.event.FocusEvent e) 
				{
					//System.out.println("focusLost()"); 
					if (numOfClustersText.getText().matches("[0-9]+")) {
						numOfClusters = Integer.parseInt(numOfClustersText.getText());
						flag1 = 0;
					}
					else
						flag1 = 2;

				}
			}
			);
			
			
			JLabel lblMaxClusterSize = new JLabel("Maximum cluster size:");
		 	lblMaxClusterSize.setBounds(55, 350, 140, 25);
		
			maxClusterSizeText = new JTextField();
			maxClusterSizeText.setBounds(new Rectangle(220, 350, 150, 25));
			maxClusterSizeText.setText("100");
			maxClusterSizeText.addFocusListener(new java.awt.event.FocusAdapter() 
			{
				public void focusGained(FocusEvent e){
					maxClusterSizeText.setText("");
		        }
				public void focusLost(java.awt.event.FocusEvent e) 
				{
					//System.out.println("focusLost()"); 
					if (maxClusterSizeText.getText().matches("[0-9]+")) {
						maxClusterSize=Integer.parseInt(maxClusterSizeText.getText());		
						flag2 = 0;
					}
					else
						flag2 = 2;
				}
			}
			);
			
			JLabel lblNumOfItr = new JLabel("Number of Iterations:");
			lblNumOfItr.setBounds(55, 400, 128, 25);
			numOfItrText = new JTextField();
			numOfItrText.setBounds(new Rectangle(220, 400, 150, 25));
			numOfItrText.setText("100");
			numOfItrText.addFocusListener(new java.awt.event.FocusAdapter() 
			{
				public void focusGained(FocusEvent e){
					numOfItrText.setText("");
		        }
				public void focusLost(java.awt.event.FocusEvent e) 
				{
					//System.out.println("focusLost()"); 
					if (numOfItrText.getText().matches("[0-9]+")) {
						numOfItr = Integer.parseInt(numOfItrText.getText());
						flag3= 0;
					}
					else
						flag3 = 2;
				}
			}
			);
		
		
			
		
		 JLabel headline3 = new JLabel("Efficient visual search of videos cast as text retrieval");
		 headline3.setFont(new Font("Serif", Font.BOLD, 26));
		 headline3.setBounds(20, 60, 1000, 100);
		 
		 
		 JButton button3 = new JButton();
		 button3.setText("Back");
		 button3.setBounds(265, 600, 100, 30);
		 button3.addActionListener(new ActionListener() {
		    	 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		            System.out.println("You clicked the 'Back' button");
		            //remove(panel);
		        	panel = createPanel(1);
		        	setContentPane(panel);             
			        setVisible(true);
		        }
		    });
		 
         panel2.add( buildDB, gbc);
		 panel2.add(TextPath);
		 panel2.add(lblFolder);
		 panel2.add(TextDbId);
		 panel2.add(lblMaxClusterSize);
		 panel2.add(lblNumOfClusters);
		 panel2.add(lblNumOfItr);
		 panel2.add(lblID);
		// panel2.add(buildDB);
		 panel2.add(chooseDB);
		 panel2.add(helpButton);
		 
		 panel2.add(headline3);
		 panel2.add(button3);
		 panel2.add(Label1);
		 panel2.add(maxClusterSizeText);
		 panel2.add(numOfClustersText);
		 panel2.add(numOfItrText);
		 return panel2;
    }
    
    public JPanel VisualSearch()
    {

    	 JLabel headline = new JLabel("Efficient visual search of videos cast as text retrieval");
 		headline.setFont(new Font("Serif", Font.BOLD, 26));
 	    headline.setBounds(20, 60, 1000, 100);
 	    
 	    ImageIcon icon = new ImageIcon("c:/project/crop/BraudeLogo.JPG");
 	    JLabel Label1 = new JLabel(icon, JLabel.CENTER);
 		Label1.setBounds(210, 7, 250, 100);
 		
		 	panel3 = new JPanel();
		 	panel3.setLayout(null);
		 	panel3.setBackground(Color.WHITE);
		 	
			JLabel lblDBChoose = new JLabel("Choose Data Base:");
			lblDBChoose.setBounds(75, 300, 180, 25);
		 
			JLabel lblNumOfImg = new JLabel("Number of image to retrieve:");
			lblNumOfImg.setBounds(75, 350, 180, 25);
			numOfImgText = new JTextField();
			numOfImgText.setBounds(new Rectangle(265, 350, 100, 25));
			numOfImgText.setText("44");
			numOfImgText.addFocusListener(new java.awt.event.FocusAdapter() 
				{
				public void focusGained(FocusEvent e){
					numOfImgText.setText("");
		        }
					public void focusLost(java.awt.event.FocusEvent e) 
					{
						System.out.println("focusLost()"); 
						numOfImg = Integer.parseInt(numOfImgText.getText());
					}
				}
			);
			
			
			JButton helpButton = new JButton();
			helpButton.setText("Help");
			helpButton.setBounds(20, 20, 60, 30); 
			helpButton.addActionListener(new ActionListener() {
		    	 
		        public void actionPerformed(ActionEvent e)
		        {
		        	JFrame helpFrame = new JFrame();
		        	helpFrame.setSize(440, 350);
		        	helpFrame.setLocation(450, 250);       
		            JPanel helpPanel = new JPanel(null);
		            helpPanel.setLayout(null);
		            helpPanel.setBackground(Color.WHITE);
		            
		            ImageIcon helpIcon = new ImageIcon("c:/project/crop/VisualSearchHelp.png");
		    	    JLabel Label1 = new JLabel(helpIcon);
		    		Label1.setBounds(0, 0, 425, 350);
		            
		    		helpPanel.add(Label1);
		            helpFrame.add(helpPanel);
		            helpFrame.setVisible(true);
		          
		     

		        }
		    });
			
			
		 	chooseImg = new JButton();
		 	chooseImg.setText("choose image");
		 	chooseImg.setBounds(130, 500, 150, 30);
		 	//imgChooser.setApproveButtonText("Select"); 
		 	chooseImg.addActionListener(new ActionListener() {
		    	 
		        public void actionPerformed(ActionEvent e)
		        {
		        	
		            //Execute when button is pressed
		            System.out.println("You clicked the 'chooseImg' button");
		            final FileNameExtensionFilter filter =  new FileNameExtensionFilter("JPG, GIF, & PNG Images", "jpg", "gif", "png");   
		            
		           
		            imgChooser.setFileFilter(filter);  
		            imgChooser.setCurrentDirectory(theDirectory);
		            
		         	Integer opt = imgChooser.showSaveDialog(imgChooser);	
		         	
		         	if(opt==0){
				 	imgFile = imgChooser.getSelectedFile();
		        	new CropImage().start(imgFile.getPath()); 
		         	}
		        }
		    });
	
		 	searchButton = new JButton();
		 	searchButton.setText("Search");
		 	searchButton.setBounds(330, 500, 150, 30);
		 	searchButton.addActionListener(new ActionListener() {
		    	 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		            System.out.println("You clicked the 'searchButton' button");
		            BufferedImage img = null;
		        	try {
		    			img = ImageIO.read(new File("c:\\project\\crop\\Crop.jpg"));
		    		} catch (IOException e2) {
		    			
		    			e2.printStackTrace();
		    		}

		    	  	QueryImage Q = new QueryImage(img, selectedDB, (double)matchPrec/100, numOfImg);
		    	  	resultList = Q.start();
		    	  	if (resultList.size()>0)
		    	  	{
		    	  		totalPages = resultList.size()/6;
		    	  		currentPage = 0;
		    	  		panel = createPanel(4);
			        	setContentPane(panel);             
				        setVisible(true);
		    	  	}
		    	  		
		        }
		    });
		 	
		
			JLabel lblMatchPrec = new JLabel("Match precentage (0-100):");
			lblMatchPrec.setBounds(75, 400, 180, 25);
		
			matchPrecText = new JTextField();
			matchPrecText.setBounds(new Rectangle(265, 400, 100, 25));
			matchPrecText.setText("0");
			matchPrecText.addFocusListener(new java.awt.event.FocusAdapter() 
			{
				public void focusGained(FocusEvent e){
					matchPrecText.setText("");
		        }
				public void focusLost(java.awt.event.FocusEvent e) 
				{
					System.out.println("focusLost()"); 
					matchPrec=Integer.parseInt(matchPrecText.getText());	
				}
			}
			);
		 
		 
		 ImageIcon cropImg = new ImageIcon("c:\\project\\crop\\Crop.jpg");
		 JLabel cropImgLbl = new JLabel(cropImg);
		 cropImgLbl.setBounds(75, 160, 149, 117);
		
		 
		 ImageIcon image1 = new ImageIcon("c:/project/crop/cow.jpg");
		 JButton img1but = new JButton(image1);
		 img1but.setBounds(75, 160, 149, 117);
		 img1but.addActionListener(new ActionListener() {
			 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		        	String name ="c:/project/crop/cow.JPG";
		        	new CropImage().start(name); 
		        }
		    });
		 
		 ImageIcon image2 = new ImageIcon("c:/project/crop/whiteflower.JPG");
		 JButton img2but = new JButton(image2);
		 img2but.setBounds(245, 160, 149, 117);
		 img2but.addActionListener(new ActionListener() {
			 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		        	String name ="c:/project/crop/whiteflower.JPG";
		        	new CropImage().start(name); 
		        }
		    });
		 
		 ImageIcon image3 = new ImageIcon("c:/project/crop/bigben.JPG");
		 JButton img3but = new JButton(image3);
		 img3but.setBounds(415, 160, 149, 117);
		 img3but.addActionListener(new ActionListener() {
			 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		        	String name ="c:/project/crop/bigben.JPG";
		        	new CropImage().start(name); 
		        }
		    });
		 
		
		 
		 
		 JButton button4 = new JButton();
		 button4.setText("Back");
		 button4.setBounds(265, 600, 100, 30);
		 button4.addActionListener(new ActionListener() {
			 
			 
		        public void actionPerformed(ActionEvent e)
		        {
		            //Execute when button is pressed
		            System.out.println("You clicked the 'Back' button");
		            panel = createPanel(1);
		        	setContentPane(panel);             
			        setVisible(true);
		        }
		    });    
		
		 panel3.add(searchButton);
		 panel3.add(lblMatchPrec);
		 panel3.add(lblNumOfImg);
		 panel3.add(chooseImg);
		 panel3.add(matchPrecText);
		 panel3.add(numOfImgText);
		 panel3.add(headline);
		 panel3.add(Label1);
		 panel3.add(button4);
		 panel3.add(getDataBaseCombo());
		 panel3.add(lblDBChoose);
		 panel3.add(helpButton);
		 panel3.add(img1but);
		 panel3.add(img2but);
		 panel3.add(img3but);
		 
		 
		 
		 return panel3;
    }

 /*   public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Gui1().setVisible(true);
            }
        });
    }
    */
    private JComboBox getDataBaseCombo() {
		if (dataBaseCombo == null) {
			ArrayList<String> dataBaseList = new ArrayList<String>();
			dataBaseList = getDataBaseList();
			dataBaseCombo = new JComboBox();
			dataBaseCombo.setBounds(new Rectangle(265, 300, 100, 25));
			if (dataBaseList.isEmpty())
				dataBaseCombo.addItem("There is no data base");
			else
			{
				selectedDB = dataBaseList.get(0);
				for (int i = 0; i < dataBaseList.size(); i++)
					dataBaseCombo.addItem(dataBaseList.get(i));
				
				dataBaseCombo.addItemListener(new java.awt.event.ItemListener() {
					public void itemStateChanged(java.awt.event.ItemEvent e) {
						System.out.println("itemStateChanged()"); 
						selectedDB = dataBaseCombo.getSelectedItem().toString();
						System.out.println("this is : " + selectedDB); 
					}
				});
			}
		}
		return dataBaseCombo;
	}

private ArrayList<String> getDataBaseList() {
	ArrayList<String> dataBaseList = new ArrayList<String>();
	try
	{
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/objectsretrival","root","omer11");	
		Statement stmt = conn.createStatement();
		
		// check database id
		System.out.println("get database id");
		ResultSet res=stmt.executeQuery("SELECT DataBaseID FROM databaset;");			
		while (res.next())
			dataBaseList.add(res.getString(1));
		}
	catch (SQLException e) {e.printStackTrace();}
	return dataBaseList;
}


private static BufferedImage resizeImage(BufferedImage originalImage, int type){
	BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
	Graphics2D g = resizedImage.createGraphics();
	g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
	g.dispose();
 
	return resizedImage;
    }



}



